using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using Common;

using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Tabs;
using DotNetNuke;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Common.Utilities;


public partial class controls_tm_showAdvert : System.Web.UI.UserControl
{
    public int myTabId = Null.NullInteger;
    public int myPortalId = Null.NullInteger;
    public int myUserId = Null.NullInteger;
    public string myTabName = Null.NullString;
    //static int advertTabId;
    //static int advertModuleId;
    private int _myHeight = 0, _myWidth = 0;
  
    public int myHeight
    {
        set
        {
            _myHeight = value;
        }
    }

    public int myWidth
    {
        set
        {
            _myWidth = value;
        }
    }



    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            
            //advertTabId = appLibs.GetTabIdByName(myPortalId, myTabName);
            //advertModuleId = appLibs.GetModuleIdByName(myPortalId, myTabName);
        }
    }

    #region Protected Methods

    public bool GetAdvert(int position)
    {
        int ownerId = appLibs.GetOwnerIdFromStore(myPortalId);
        ownerId = 3530;
        string sqlCMD = "SELECT     advertId, description, altDescription, advertFile, url " +
                        " FROM         dbo.tm_advert "+
                        " WHERE (GETDATE() BETWEEN tm_advert.openDate AND tm_advert.closeDate+1) " +
                        " AND tm_advert.advertPos = " + position + " AND tm_advert.status = '" + consts.constStatusENABLE + "' AND (tm_advert.createdBy = " + ownerId + ")"; 

        if (myTabId <= 0) return false;
        dsAdvert.ConnectionString = sysLibs.GetConnectionString;
        dsAdvert.SelectCommand = sqlCMD;
        dsAdvert.Select(DataSourceSelectArguments.Empty);
        gvAdvert.DataBind();
        if (gvAdvert.Rows.Count > 0)
            return true;
        else
            return false;
    }

    protected string ShowImage(string advertId, string url, string advertFile, string altDescription, string description)
    {
        string URL = "";
        string image = "", a = "";

        a = "<a href='" + GetURL(advertId, url.Trim()) + "' target='_blank'>";
        image = "<img border='0' align='lef' alt='" + altDescription + "'  src='" + getImage(advertFile) + "'" + getWidth() + getHight() + "/>";
        URL = a + image + "</a> <br />";
        return URL;

    }

    protected string getImage(string fileName)
    {
        int ownerId = appLibs.GetOwnerIdFromStore(myPortalId);
        //ownerId = 3530;
        string loc_filePath = GetLinkStoreAdvert(ownerId) + "/" + sysLibs.GetHashString(fileName.Trim()) + "/" + fileName;
        try
        {
            if (!File.Exists(Server.MapPath(loc_filePath)))
                loc_filePath = consts.constNoImageFileName;
        }
        catch (Exception exc)
        {
            Exceptions.ProcessModuleLoadException(this, exc);
        }
        return loc_filePath;
    }

    protected string getWidth()
    {
        return _myWidth > 0 ? " width = '" + _myWidth + "'" : "";
    }

    protected string getHight()
    {
        return _myHeight > 0 ? " height = '" + _myHeight + "'" : "";
    }

    protected string GetURL(string infoId,string url)
    {
        //return DotNetNuke.Common.Globals.NavigateURL(advertTabId, "link", "mid=" + advertModuleId, "tabAdvert=" + myTabId, "infoId=" + infoId, "userId=" + myUserId);
        return "";
    }
    protected string GetLinkStoreAdvert(int ownerId)
    {
        SqlConnection conn = null;
        string portalId = "";
        try
        {
            string sql = " SELECT portalId FROM tm_store " +
                                " WHERE ownerId = " + ownerId;


            conn = new SqlConnection(Common.sysLibs.GetConnectionString);
            conn.Open();

            SqlCommand comm = new SqlCommand(sql, conn);

            SqlDataReader reader;
            reader = comm.ExecuteReader();
            if (reader.Read()) portalId = reader["portalId"].ToString().Trim();
            if (portalId != "")
            {
                return ("/Portals/" + portalId);
            }

        }
        catch { }
        finally
        {
            if (conn != null)
                conn.Close();
        }

        return "";
    }
#endregion
}
